package com.zk.shop.servlet.Cart;

import com.zk.shop.entity.CartItem;
import com.zk.shop.entity.Goods;
import com.zk.shop.entity.User;
import com.zk.shop.service.CartService;
import com.zk.shop.service.Impl.CartServiceImpl;
import com.zk.shop.util.CartCookieUtil;
import com.zk.shop.util.JSONServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@WebServlet("/UpdateCartServlet")
public class UpdateCartServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = new CartServiceImpl();

        int goodsId = Integer.parseInt(request.getParameter("goodsId"));
        //当前商品剩余购买数量
        int buyNum = Integer.parseInt(request.getParameter("buyNum"));
        User user = (User) request.getSession().getAttribute("user");
        CartItem cartItem = new CartItem(user,new Goods(goodsId),buyNum);

        List<CartItem> cartItemList = CartCookieUtil.getCart(request);
        CartItem delCartItem = null;
        if(user == null){
            // 判断购物车列表是否为空
            if (cartItemList.size() > 0) {
                // 循环购物车列表寻找相同ID的商品
                for (CartItem item : cartItemList) {
                    if (item.getGoods().getGoodsId() == goodsId) {
                        delCartItem = item;
                        delCartItem.setBuyNum(buyNum);
                        break;
                    }
                }
                if(delCartItem != null){
                    if(buyNum > 0){
                        cartItemList.remove(delCartItem);
                        cartItemList.add(delCartItem);
                    }else{
                        cartItemList.remove(delCartItem);
                    }
                    if(cartItemList.size() == 0){
                        //如果购物车为空，删除cookie
                        Cookie cookie = new Cookie("cart",null);//cookie名字要相同
                        cookie.setMaxAge(0); //
                        cookie.setPath(request.getContextPath());  // 相同路径
                        response.addCookie(cookie);
                    }else{
                        //不为空替换
                        Cookie cookie = CartCookieUtil.getCookie(request);
                        // 为cookie设置value
                        cookie.setValue(URLEncoder.encode(CartCookieUtil.makeCookieValue(cartItemList), "UTF-8"));
                        // 设置寿命
                        cookie.setMaxAge(60 * 10);
                        // 设置路径
                        cookie.setPath("/");
                        // 更新cookie
                        response.addCookie(cookie);
                    }

                }
            }
        } else{
            //如果购买数量大于0 更新数量信息
            if(buyNum > 0){
                cartService.updateBuyNum(cartItem);
            }else{  //移除该购物车信息
                cartService.delCartItem(cartItem);
            }
        }
        JSONServlet.getJson(request,response,true);
    }
}
